package com.lgcns.andteam2.todaymenu.db;

import java.util.Arrays;
import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

/**
 * 메뉴 정보를 제공하는 ContentPriovider.
 * CP를 사용할 경우 DB update시 다운로드 받은 DB 파일을 기존파일로 교체할 경우 에러 발생.
 * DB 파일을 기존파일로 교체하지 않고 select & insert시 시간 과다 소요됨(3개 사업장 3~4초 소요)
 * ==> CP를 사용하지 않도록 함.
 */
public class MenuProvider_notuse extends ContentProvider {
    private static final String TAG = MenuProvider_notuse.class.getSimpleName();

    private static final UriMatcher mUriMatcher;
    private static final String AUTHOIRTY = "com.lgcns.andteam2.todaymenu";
    // Uri path
    private static final String PLACE_PATH = "places";
    private static final String DIETITIAN_PATH = "dietitians";
    private static final String TODAY_PATH = "today";
    private static final String WEEK_PATH = "week";
    private static final String DB_UPDATE_PATH = "dbupdate";
    // Uri
    public static final Uri PLACE_CONTENT_URI = Uri.parse("content://" + AUTHOIRTY + "/" + PLACE_PATH);
    public static final Uri DIETITIAN_CONTENT_URI = Uri.parse("content://" + AUTHOIRTY + "/" + DIETITIAN_PATH);
    public static final Uri TODAY_CONTENT_URI = Uri.parse("content://" + AUTHOIRTY + "/" + TODAY_PATH);
    public static final Uri WEEK_CONTENT_URI = Uri.parse("content://" + AUTHOIRTY + "/" + WEEK_PATH);
    public static final Uri DB_UPDATE_CONTENT_URI = Uri.parse("content://" + AUTHOIRTY + "/" + DB_UPDATE_PATH);
    // uri code(int)
    private static final int PLACE_ID = 1;
    private static final int PLACE = 2;
    private static final int DIETITIAN = 3;
    private static final int TODAY = 4;
    private static final int WEEK = 5;
    private static final int DB_UPDATE = 6;

    // projection
    private static HashMap<String, String> PLACE_PROJECTION;
    private static HashMap<String, String> DIETITIAN_PROJECTION;
    private static HashMap<String, String> MENU_PROJECTION;
    
    /**
     * TB_PLACE 테이블 컬럼, WHERE, ORDER BY 정보
     */
    public static final class TbPlace implements BaseColumns {
        public static final String TABLE_NAME = "tb_place";
        public static final String PLACE = "place";
        public static final int INDEX_ID = 0;
        public static final int INDEX_PLACE = 1;
        /** _ID + " = ?" */
        public static final String WHERE_CLAUSE = _ID + " = ?";
        public static final String ORDER_BY = PLACE;
    }
    
    /**
     * TB_DIETITIAN 테이블 컬럼, WHERE, ORDER BY 정보
     */
    public static final class TbDietitian implements BaseColumns {
        public static final String TABLE_NAME = "tb_dietitian";
        public static final String PLACE_ID = "place_id";
        public static final String DIETITIAN_NAME = "dietitian_name";
        public static final String EMAIL = "email";
        /** PLACE_ID + " = ?" */
        public static final String WHERE_CLAUSE = PLACE_ID + " = ?";
    }
    
    /**
     * TB_MENU 테이블 컬럼, WHERE, ORDER BY 정보
     */
    public static final class TbMenu implements BaseColumns {
        public static final String TABLE_NAME = "tb_menu";
        public static final String PLACE_ID = "place_id";
        public static final String MENU_DATE = "menu_date";
        public static final String MENU_DAY = "menu_day";
        public static final String GUBUN_CODE = "gubun_code";
        public static final String GUBUN = "gubun";
        public static final String CORNER = "corner";
        public static final String MENU_NAME = "menu_name";
        public static final String CAL = "cal";
        public static final String SIDE_MENU01 = "side_menu_01";
        public static final String SIDE_MENU02 = "side_menu_02";
        public static final String SIDE_MENU03 = "side_menu_03";
        public static final String SIDE_MENU04 = "side_menu_04";
        public static final String SIDE_MENU05 = "side_menu_05";
        public static final String SIDE_MENU06 = "side_menu_06";
        public static final String SIDE_MENU07 = "side_menu_07";
        public static final String DESSERT = "dessert";
        /** PLACE_ID + " = ? " + MENU_DATE + " = ? " + GUBUN_CODE + " = ?" */
        public static final String TODAY_WHERE_CLAUSE = PLACE_ID + " = ? AND " + MENU_DATE + " = ? AND " + GUBUN_CODE + " = ?";
        public static final String TODAY_ORDER_BY = CORNER;
        /** PLACE_ID + " = ?" */
        public static final String WEEK_WHERE_CLAUSE = PLACE_ID + " = ?";
        public static final String WEEK_ORDER_BY = MENU_DATE + ", " + GUBUN_CODE + ", " + CORNER;
    }
    
    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(AUTHOIRTY, PLACE_PATH, PLACE);
        mUriMatcher.addURI(AUTHOIRTY, PLACE_PATH + "/#", PLACE_ID);
        mUriMatcher.addURI(AUTHOIRTY, DIETITIAN_PATH, DIETITIAN);
        mUriMatcher.addURI(AUTHOIRTY, TODAY_PATH, TODAY);
        mUriMatcher.addURI(AUTHOIRTY, WEEK_PATH, WEEK);
        mUriMatcher.addURI(AUTHOIRTY, DB_UPDATE_PATH, DB_UPDATE);
        
        PLACE_PROJECTION = new HashMap<String, String>();
        PLACE_PROJECTION.put(TbPlace._ID, TbPlace._ID);
        PLACE_PROJECTION.put(TbPlace.PLACE, TbPlace.PLACE);

        DIETITIAN_PROJECTION = new HashMap<String, String>();
        DIETITIAN_PROJECTION.put(TbDietitian._ID, TbDietitian._ID);
        DIETITIAN_PROJECTION.put(TbDietitian.PLACE_ID, TbDietitian.PLACE_ID);
        DIETITIAN_PROJECTION.put(TbDietitian.DIETITIAN_NAME, TbDietitian.DIETITIAN_NAME);
        DIETITIAN_PROJECTION.put(TbDietitian.EMAIL, TbDietitian.EMAIL);

        MENU_PROJECTION = new HashMap<String, String>();
        MENU_PROJECTION.put(TbMenu._ID, TbMenu._ID);
        MENU_PROJECTION.put(TbMenu.PLACE_ID, TbMenu.PLACE_ID);
        MENU_PROJECTION.put(TbMenu.MENU_DATE, TbMenu.MENU_DATE);
        MENU_PROJECTION.put(TbMenu.MENU_DAY, TbMenu.MENU_DAY);
        MENU_PROJECTION.put(TbMenu.GUBUN_CODE, TbMenu.GUBUN_CODE);
        MENU_PROJECTION.put(TbMenu.GUBUN, TbMenu.GUBUN);
        MENU_PROJECTION.put(TbMenu.CORNER, TbMenu.CORNER);
        MENU_PROJECTION.put(TbMenu.MENU_NAME, TbMenu.MENU_NAME);
        MENU_PROJECTION.put(TbMenu.CAL, TbMenu.CAL);
        MENU_PROJECTION.put(TbMenu.SIDE_MENU01, TbMenu.SIDE_MENU01);
        MENU_PROJECTION.put(TbMenu.SIDE_MENU02, TbMenu.SIDE_MENU02);
        MENU_PROJECTION.put(TbMenu.SIDE_MENU03, TbMenu.SIDE_MENU03);
        MENU_PROJECTION.put(TbMenu.SIDE_MENU04, TbMenu.SIDE_MENU04);
        MENU_PROJECTION.put(TbMenu.SIDE_MENU05, TbMenu.SIDE_MENU05);
        MENU_PROJECTION.put(TbMenu.SIDE_MENU06, TbMenu.SIDE_MENU06);
        MENU_PROJECTION.put(TbMenu.SIDE_MENU07, TbMenu.SIDE_MENU07);
        MENU_PROJECTION.put(TbMenu.DESSERT, TbMenu.DESSERT);
    }
    
    private MenuDBHelper mDbHelper;
    SQLiteDatabase db;

    @Override
    public boolean onCreate() {
        Log.d(TAG, "onCreate()");
        mDbHelper = new MenuDBHelper(getContext());
        db = mDbHelper.getWritableDatabase();
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        
        switch(mUriMatcher.match(uri)) {
            case PLACE_ID:
                qb.setTables(TbPlace.TABLE_NAME);
                qb.setProjectionMap(PLACE_PROJECTION);
                break;
            case PLACE:
                qb.setTables(TbPlace.TABLE_NAME);
                qb.setProjectionMap(PLACE_PROJECTION);
                break;
            case DIETITIAN:
                qb.setTables(TbDietitian.TABLE_NAME);
                qb.setProjectionMap(DIETITIAN_PROJECTION);
                break;
            case TODAY:
                qb.setTables(TbMenu.TABLE_NAME);
                qb.setProjectionMap(MENU_PROJECTION);
                break;
            case WEEK:
                qb.setTables(TbMenu.TABLE_NAME);
                qb.setProjectionMap(MENU_PROJECTION);
                break;
            default:
                break;
        }
        
        Log.d(TAG, "Query : " + qb.buildQuery(projection, selection, selectionArgs, null, null, sortOrder, null));
        Log.d(TAG, "params : " + Arrays.toString(selectionArgs));
        
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        c.setNotificationUri(getContext().getContentResolver(), uri);
        
        return c;
    }

    @Override
    public String getType(Uri uri) {
        switch(mUriMatcher.match(uri)) {
            case PLACE_ID:
                return "vnd.android.cursor.item/vnd.andteam2.place";
            case PLACE:
                return "vnd.android.cursor.dir/vnd.andteam2.places";
            case DIETITIAN:
                return "vnd.android.cursor.dir/vnd.andteam2.dietitians";
            case TODAY:
                return "vnd.android.cursor.dir/vnd.andteam2.menus";
            case WEEK:
                return "vnd.android.cursor.dir/vnd.andteam2.menus";
            default:
                return null;
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        Log.d(TAG, "update()");

        // DB에 변경사항이 발생한 것을 알림.
        getContext().getContentResolver().notifyChange(uri, null);
        return 0;
    }
}
